<?php

require_once('../../Config/config.php');
require_once("qqConnectAPI.php");
$qc = new QC();
$back = $qc->qq_callback();
$openid = $qc->get_openid();

if($openid){
	require '../MyPdo.class.php';
	$user_account = 'qq_'.$openid;
	$pdo = MyPdo::init();
	$sql = "select user_id,user_ltime from {$config['table_prefix']}user where user_name = ?";
	$res = $pdo->dql($sql,array($user_account),1);
	if($res){  //如果用户以存在
		$ltime = date('Ymd',$res['user_ltime']);	//获取记录的最后登陆日期
		$today = date('Ymd');	//获取当前日期
		if($ltime < $today){   //判断当天是否以登陆过，没有就记录，有则跳过
			$time = time();
			$ip = $_SERVER['REMOTE_ADDR'];
			$sql = "update {$config['table_prefix']}user set user_ltime=? where user_name='{$user_account}'";
			$res2 = $pdo->dml($sql,array($time));
			if($res2){
				$sql = "insert into {$config['table_prefix']}login_record values(null,?,?,?)";
				$res3 = $pdo->dml($sql,array($res['user_id'],$ip,$time));
			}
		}
		setCookie('uid',$res['user_id'],time()+36000,'/');
	}else{  //如果用户不存在就添加
		$ip = $_SERVER['REMOTE_ADDR'];
		$time = time();
		$sql = "insert into {$config['table_prefix']}user(user_name,user_ctime,user_ltime) values(?,?,?)";
		$res = $pdo->dml($sql,array($user_account,$time,$time));
		if($res > 0){
			$sql2 = "insert into {$config['table_prefix']}user_normal(uid) values(?)";
			$res2 = $pdo->dml($sql2,array($res));
			setCookie('uid',$res,time()+36000,'/');
			$sql = "insert into {$config['table_prefix']}login_record values(null,?,?,?)";
			$res = $pdo->dml($sql,array($res,$ip,$time));
		}
	}

}
header('location:../../index.html');















/*
require_once('../../Config/config.php');
require_once("qqConnectAPI.php");
$qc = new QC();
$back = $qc->qq_callback();
$openid = $qc->get_openid();

if($openid){
	require '../MyPdo.class.php';
	$user_account = 'qq_'.$openid;
	$pdo = MyPdo::init();
	$sql = 'select user_id,user_ltime,user_login from {$config['table_prefix']}user where user_name = ?';
	$res = $pdo->dql($sql,array($user_account),1);
	if($res){  //如果用户以存在
		$ltime = date('Ymd',$res['user_ltime']);	//获取记录的最后登陆日期
		$today = date('Ymd');	//获取当前日期
		//echo $ltime.'<br>'.$today;exit;
		if($ltime < $today){   //判断当天是否以登陆过，没有就记录，有则跳过
			$infos = json_decode($res['user_login'],1);
			//$info['ip'] = ip2long($_SERVER['REMOTE_ADDR']);
			$info['ip'] = $_SERVER['REMOTE_ADDR'];
			$info['time'] = time();
			array_push($infos,$info);
			$infos_str = json_encode($infos);
			$sql = "update {$config['table_prefix']}user set user_login=?,user_ltime=? where user_name='{$user_account}'";
			$res2 = $pdo->dml($sql,array($infos_str,strtotime($today)));
		}
		//echo $res['user_login'];
		setCookie('uid',$res['user_id'],time()+36000,'/');
	}else{  //如果用户不存在就添加
		$infos = array();
		//$info['ip'] = ip2long($_SERVER['REMOTE_ADDR']);
		$info['ip'] = $_SERVER['REMOTE_ADDR'];
		$info['time'] = time();
		array_push($infos,$info);
		
		$info_str = json_encode($infos);
		$sql = 'insert into {$config['table_prefix']}user(user_name,user_ctime,user_ltime,user_login) values(?,?,?,?)';
		$res = $pdo->dml($sql,array($user_account,time(),time(),$info_str));
		//var_dump($res);
		if($res > 0){
			setCookie('uid',$res,time()+36000,'/');

		}
	}

}
header('location:../../index.html');
*/